using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Scene
{
	public class NetSceneGetMFriend : NetSceneBaseEx<GetMFriendRequest, GetMFriendResponse, GetMFriendRequest.Builder>
	{
		private const string TAG = "NetSceneGetMFriend";
		public const int MM_MOBILE_FRIEND = 0;
		public const int MM_FACEBOOK_FRIEND = 1;
		public bool doScene(int opType = 0)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.OpType = (uint)opType;
			this.mBuilder.MD5 = "";
			if (opType == 1)
			{
				ConfigMgr.getString("FBFriendListMd5", "");
				this.mBuilder.MD5 = "";
			}
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/getmfriend";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(GetMFriendRequest request, GetMFriendResponse response)
		{
			Log.smethod_1("NetSceneGetMFriend", "Get my friend failed system error!");
			EventCenter.postEvent(EventConst.ON_NETSCENE_GETFRIEND_FAIL, RetConst.MM_ERR_SYS, null);
		}
		protected override void onSuccess(GetMFriendRequest request, GetMFriendResponse response)
		{
			if (response == null || response.BaseResponse.Ret != 0)
			{
				Log.smethod_1("NetSceneGetMFriend", "Get my friend response not invalidate ret = " + ((RetConst)response.BaseResponse.Ret).ToString());
				EventCenter.postEvent(EventConst.ON_NETSCENE_GETFRIEND_FAIL, response.BaseResponse.Ret, null);
				return;
			}
			Log.smethod_4("NetSceneGetMFriend", "response.FriendListList.count = " + response.FriendListList.get_Count());
			List<MobileContact> list = new List<MobileContact>();
			Dictionary<string, MFriend> dictionary = new Dictionary<string, MFriend>();
			IEnumerator<MFriend> enumerator = response.FriendListList.GetEnumerator();
			if (request.OpType == 1u)
			{
				ConfigMgr.setString("FBFriendListMd5", response.MD5);
				List<FBContact> list2 = new List<FBContact>();
				long num = (long)Util.getNowSeconds();
				while (enumerator.MoveNext())
				{
					FBContact fBContact = new FBContact();
					MFriend current = enumerator.get_Current();
					fBContact.nID = current.FBInfo.ID;
					fBContact.strFacebookName = current.FBInfo.Name;
					fBContact.nImgKey = current.FBInfo.ImgKey;
					fBContact.strUsrName = current.Username;
					fBContact.strCity = current.City;
					fBContact.strAlias = current.Alias;
					fBContact.strProvince = current.Province;
					fBContact.strSignature = current.Signature;
					fBContact.strNickName = current.Nickname;
					fBContact.strNickNamePYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(current.Nickname);
					fBContact.strNickNameQuanPin = GetPinYin.ConvertStrToQuanPin(current.Nickname);
					fBContact.nSex = current.Sex;
					fBContact.nPersonalCard = current.PersonalCard;
					fBContact.nLastUpdateTime = num;
					fBContact.nSnsFlag = current.SnsUserInfo.SnsFlag;
					fBContact.strSnsBGImgID = current.SnsUserInfo.SnsBGImgID;
					fBContact.nSnsBGObjectID = current.SnsUserInfo.SnsBGObjectID;
					if (!string.IsNullOrEmpty(fBContact.strUsrName))
					{
						Contact contact = StorageMgr.contact.method_0(fBContact.strUsrName);
						if (contact != null && ContactMgr.isContact(contact))
						{
							fBContact.nStatus = 2;
						}
						else
						{
							fBContact.nStatus = 1;
						}
					}
					list2.Add(fBContact);
				}
				StorageMgr.fbContact.updateList(list2);
				EventCenter.postEvent(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, num, null);
				return;
			}
			while (enumerator.MoveNext())
			{
				MFriend current2 = enumerator.get_Current();
				if (!dictionary.ContainsKey(current2.Username))
				{
					dictionary.Add(current2.Username, current2);
					MobileContact mobileContact = StorageMgr.mobileContact.method_7(current2.MobileMD5);
					if (mobileContact != null)
					{
						mobileContact.strUsrName = current2.Username;
						mobileContact.strNickName = current2.Nickname;
						mobileContact.strNickNamePYInitial = SortStringsByAlpha.ConvertStringToPinyinInitial(current2.Nickname);
						mobileContact.strNickNameQuanPin = GetPinYin.ConvertStrToQuanPin(current2.Nickname);
						mobileContact.strCity = current2.City;
						mobileContact.strProvince = current2.Province;
						mobileContact.strSignature = current2.Signature;
						mobileContact.strAlias = current2.Alias;
						mobileContact.nSnsFlag = current2.SnsUserInfo.SnsFlag;
						mobileContact.strSnsBGImgID = current2.SnsUserInfo.SnsBGImgID;
						mobileContact.nSnsBGObjectID = current2.SnsUserInfo.SnsBGObjectID;
						mobileContact.nSex = current2.Sex;
						mobileContact.nPersonalCard = current2.PersonalCard;
						Contact contact2 = StorageMgr.contact.method_0(current2.Username);
						if (contact2 != null && contact2.strUsrName.Equals(current2.Username) && ContactMgr.isContact(contact2))
						{
							mobileContact.strNickName = contact2.strNickName;
							mobileContact.strProvince = contact2.strProvince;
							mobileContact.strCity = contact2.strCity;
							mobileContact.strSignature = contact2.strSignature;
							mobileContact.nSex = contact2.nSex;
							mobileContact.nPersonalCard = contact2.nPersonalCard;
							mobileContact.nFlag = contact2.nFlags;
							mobileContact.nStatus = 2;
						}
						else
						{
							mobileContact.nStatus = 1;
						}
						list.Add(mobileContact);
					}
				}
			}
			StorageMgr.mobileContact.updateList(list);
			EventCenter.postEvent(EventConst.ON_NETSCENE_GETFRIEND_SUCCESS, null, null);
		}
	}
}
